﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using UserNotifier;
using LucasCode.Win32;
using System.IO;


namespace ESLReminder.Modules
{
    public class DiskspaceModule : ReminderModule
    {
        public DiskspaceModule()
            : base(new PluginInfo("DiskspaceModule",
                                    new Version(1,0),
                                    "©2009 Lucas Romero",
                                    "http://code.google.com/p/quicktools/"),
                    "Warns if you are running low on disk space on your game's partition.")
        {
            
        }

        public override void PluginLoadedEx(IReminderInterface host)
        {
            host.GameStarted += new GameStartedEventHandler(m_Reminder_GameStarted);
        }

        void m_Reminder_GameStarted(IReminderInterface sender, GameStartedEventArgs args)
        {
            try
            {
                LogInfo("Getting free disk space");
                ulong a, b, c;
                Kernel32.GetDiskFreeSpaceEx(Path.GetPathRoot(args.Game.Process.MainModule.FileName), out a, out b, out c);
                LogInfo("Logical Disk FreeSpace = " + a + " bytes");
                if (a < m_FreeDiskspaceWarnThreshold * 1024 * 1024)
                    m_Reminder.AddWarning(WarnEvents.LowDiskspace, 20000);
            }
            catch (System.Exception e)
            {
                LogError(e.Message);
            }
        }
        private uint m_FreeDiskspaceWarnThreshold = 120;
        public uint FreeDiskspaceWarnThreshold
        {
            get
            {
                return m_FreeDiskspaceWarnThreshold;
            }
            set
            {
                m_FreeDiskspaceWarnThreshold = value;
            }
        }
        /*public double GetHDDFreeSpace(string drive)
        {
            //create ManagementObject, passing it the drive letter to the
            //DevideID using WQL
            ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive);
            //bind our management object
            disk.Get();
            //return the free space amount
            return Convert.ToDouble(disk["FreeSpace"]);
        }*/
    }
}
